﻿using BLL.Sys;
using DEL.Sys;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;
using Web.Models;

// For more information on enabling MVC for empty projects, visit http://go.microsoft.com/fwlink/?LinkID=397860

namespace Web.Areas.Sys.Controllers
{
    [Authorize(Roles = "super")]
    public class MenuController : SysController<Menu>
    {

        protected override void SetCurrentEntityService()
        {
            CurrentEntityService = BLLFactory.SetMenuService();
        }
        
        /// <summary>
        /// 获取功能树JSON数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetTreeData()
        {
            var menus = CurrentEntityService.GetEntitysByLambda(o => o.Id > 0).ToList();
            List<TreeModel> treeModels = new List<TreeModel>();
            foreach (var menu in menus)
            {
                treeModels.Add(new TreeModel() { Id = menu.Id.ToString(), Text = menu.Name, Parent = menu.ParentId == 0 ? "#" : menu.ParentId.ToString() });
            }
            return Json(treeModels);
        }
        /// <summary>
        /// 获取子级功能列表
        /// </summary>
        /// <returns></returns>
        public IActionResult GetChildrenByParent(int parentId=0, int pageIndex=1)
        {
            var rowCount=0;
            var totalPages = 0;             
            var result = CurrentEntityService.GetPagedEntitysByLambdaAndSort(pageIndex,PageSize, out rowCount, out totalPages, o => o.ParentId == parentId, OrderLambda, IsASC);
            
            return Json(new
            {
                rowCount = rowCount,
                pageCount = totalPages,
                rows = result,
            });
        }

        
    }
}
